<template>
  <Panel>
    <!-- 标题 -->
    <template #title>账号列表</template>
    <!-- 内容 -->
    <template>
      <!-- 按钮 -->
      <div>
        <el-row>
          <el-button type="danger">批量删除</el-button>
          <el-button type="primary" @click="toggleSelection()"
            >取消选择</el-button
          >
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="mini"
        border
      >
        <el-table-column type="selection"> </el-table-column>
        <!-- <el-table-column label="日期" width="120">
          <template slot-scope="scope">{{ scope.row.date }}</template>
        </el-table-column> -->
        <el-table-column prop="account" label="账号"> </el-table-column>
        <el-table-column
          prop="userGroup"
          label="管理权限"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="ctime" label="创建时间" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作">
          <el-button size="mini" type="primary">编辑</el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
  </Panel>
</template>

<script>
import { userList } from '../../api/user.js'

export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 2,
      total: 0,
      tableData: [],
      multipleSelection: []
    }
  },
  // 实例准备好了就发请求
  created() {
    this.getUsers()
  },
  methods: {
    async getUsers() {
      const { data, total } = await userList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      console.log(data)
      // 成功之后的赋值
      this.tableData = data
      this.total = total
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getUsers()
      /*  console.log(`每页 ${val} 条`) */
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getUsers()
      /*   console.log(`当前页: ${val}`) */
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0 40px;
}
</style>
